<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Search extends WxHomeController {

	function __construct() {
		parent::__construct();
		$this -> sidebar = "Information";
		
	}
    // class_name课程名；teacher_name教师名称
    function Index($type){       
        $data['class_name']='';
        $data['teacher_name']='';
        $data['teacher_id']='';
        $data['class_id']='';
        $data['type']=$type;
        if(isset($_GET['class_name'])){
            $data['class_name']=urldecode($_GET['class_name']);
        }else{
            $data['class_name']='';
        }
        if(isset($_GET['class_id'])){
            $data['class_id']=urldecode($_GET['class_id']);
        }
        if(isset($_GET['teacher_name'])){
            $data['teacher_name']=urldecode($_GET['teacher_name']);
        }else{
            $data['teacher_name']='';
        }
        if(isset($_GET['teacher_id'])){
            $data['teacher_id']=urldecode($_GET['teacher_id']);
        }else{
            $data['teacher_id']='';
        }    
        $this -> load ->view("home/search",$data);

    }
    // 获取查询列表数据
    function getList(){
        $type=$_POST['type'];
        $value=$_POST['value'];
        if($value!=''){
            // 获取课程数据
            if($type==SearchType::First[XPHEnum::Value]){
                $this->load->model("Class_model");
                $month=date('m',time());
                if($month==9 || $month==10 || $month==11 || $month==12 || $month==1){
                    if($month == 1){
                        $year=date('Y',time())-1;
                        $year_last=date('Y',time()); 
                    }else{
                       $year=date('Y',time());
                       $year_last=date('Y',time())+1; 
                    }  
                    // $year=date('Y',time());
                    // $year_last=date('Y',time())+1;
                    $default_academic_year=strval($year.'-'.$year_last);            
                }else if($month==2 || $month==3 || $month==4 || $month==5 || $month==6 || $month==7 || $month==8){
                    $year=date('Y',time())-1;
                    $year_last=date('Y',time());
                    $default_academic_year=strval($year.'-'.$year_last);    
                }
                $connar['where']=$this->sqlLikeEscape('name like "%??%" and elective_lesson_no like "%??%"',array($value,$default_academic_year));
                $connar['items']="name,id";
                $connar['groupby']="name";
                $data=$this->Class_model->get_list_full($connar);
            }else if($type==SearchType::Second[XPHEnum::Value]){
                // 获取教师数据
                $this->load->model('Admin_model');
                $connar['where']=$this->sqlLikeEscape('status=?? and (username like "%??%" or usercode like "%??%" )',array(CommonStatus::UnDeleted[XPHEnum::Value],$value,$value));
                $data=$this->Admin_model->get_list_full($connar);
            }
        }
              
    }


    // 0426教师信息搜索
    function teatcherIndex($attend_id){
        $data['attend_id']=$attend_id;
         $this -> load ->view("home/search1",$data);
    }
     // 0426教师信息搜索
    function getTeatcherList(){
        $this->load->model('Admin_model');
        $connar['where']=$this->sqlEscape(' status=?? and ( role_id=?? or find_in_set(??,sub_role_id))',array(CommonStatus::UnDeleted[XPHEnum::Value],WebRole::Teacher[XPHEnum::Value],WebRole::Teacher[XPHEnum::Value]));
        $connar['groupby']="db_admin.id";
        if(isset($_POST['teacher_code']) && $_POST['teacher_code']!=''){
            $connar['where'].=$this->sqlLikeEscape(' and  (username like "%??%" or usercode like "%??%" )',array($_POST['teacher_code'],$_POST['teacher_code']));
        }else{
            if(isset($_SESSION[SESS_ASSESS]['course_units'])){
                $connar['items']="db_admin.*";         
                $connar['where'].=$this->sqlEscape(' and course_units=??',array($_SESSION[SESS_ASSESS]['course_units']));
                $connar['join']=array("db_class c",'c.teacher_id=db_admin.usercode');
            }
            if(isset($_SESSION[SESS_ASSESS]['listen_time'])){               
                $connar['items']="db_admin.*";          
                $weekarray=["周日","周一","周二","周三","周四","周五","周六"];
                $weekday=$weekarray[date("w",strtotime($_SESSION[SESS_ASSESS]['listen_time']))];
                $quantumsw=date('H',strtotime($_SESSION[SESS_ASSESS]['listen_time']));
                $quantumsw=$quantumsw<=12 ? '上午' :'下午';
                $connar['where'].=$this->sqlLikeEscape(' and sksj1 like "%??%"',array($weekday.$quantumsw));
                 $connar['join']=array("db_class c",'c.teacher_id=db_admin.usercode');
            }
            if(isset($_SESSION[SESS_ASSESS]['class_name'])){
                $connar['items']="db_admin.*";         
                $connar['where'].=$this->sqlLikeEscape(' and c.name like "%??%"',array($_SESSION[SESS_ASSESS]['class_name']));
                 $connar['join']=array("db_class c",'c.teacher_id=db_admin.usercode');
            }
        }
        
        $data=$this->Admin_model->get_list_full($connar);
        exit(retJson('返回搜索结果',true,$data)) ; 
    }
    // 0507 教师信息session保存
    function setTeacher(){
        $this->load->model("Admin_model");
        if(isset($_POST['teacher_id'])){            
            $data=$this->Admin_model->get_single(array('id'=>$_POST['teacher_id']));
            $_SESSION[SESS_ASSESS]['teacher_id']=$data['id'];
            $_SESSION[SESS_ASSESS]['teacher_name']=$data['username'];
            $_SESSION[SESS_ASSESS]['teacher_code']=$data['usercode'];
            $_SESSION[SESS_ASSESS]['title']=$data['title'];
            $_SESSION[SESS_ASSESS]['age_groups']=$data['age_groups'];
            if($data['age']){
                $_SESSION[SESS_ASSESS]['age']=$data['age'];
            }else{
                $_SESSION[SESS_ASSESS]['age']='无';
            }
            
            $_SESSION[SESS_ASSESS]['teachers_attribute']=$data['teachers_attribute'];
            $_SESSION[SESS_ASSESS]['dept_name']=$data['dept_name'];
            exit(retJson('保存成功',true)); 
        }
        exit(retJson('系统错误',false)); 
        
    }

   /*  // 0507课程信息搜索
    function classIndex($attend_id,$teacher_code=false){        
        if($teacher_code){
            $data['teacher_code']=$teacher_code;
        }else{
            $data=array();
        }
        $data['attend_id']=$attend_id;
        $this -> load ->view("home/search2",$data);
    } */
     // 202109010课程信息搜索
     function classIndex($attend_id,$course_units=false,$listen_time=false){       
        if($course_units && urldecode($course_units) != '0'){
            $data['course_units']=urldecode($course_units);
            $_SESSION[SESS_ASSESS]['course_units']=urldecode($course_units);
        }else{
            $data=array();
        }
         if($listen_time && urldecode($listen_time) != '0'){
            $_SESSION[SESS_ASSESS]['listen_time']=urldecode($listen_time);
        }

        $data['listen_time']=urldecode($listen_time) ? urldecode($listen_time) : array();
        $data['attend_id']=$attend_id;
        $this -> load ->view("home/search2",$data);
    }
     // 0507课程信息搜索
    function getClassList(){ 
        $this->load->model("Class_model");
        $weekarray=["周日","周一","周二","周三","周四","周五","周六"];

        $month=date('m',time());
        if($month==9 || $month==10 || $month==11 || $month==12 || $month==1){
            if($month == 1){
                $year=date('Y',time())-1;
                $year_last=date('Y',time()); 
            }else{
               $year=date('Y',time());
               $year_last=date('Y',time())+1; 
            }        
            $default_academic_year=strval($year.'-'.$year_last);            
        }else if($month==2 || $month==3 || $month==4 || $month==5 || $month==6 || $month==7 || $month==8){
            $year=date('Y',time())-1;
            $year_last=date('Y',time());
            $default_academic_year=strval($year.'-'.$year_last);    
        }
        $connar['where']=$this->sqlLikeEscape('elective_lesson_no like "%??%"',array($default_academic_year));
        if(isset($_POST['class_name'])&&$_POST['class_name'] != ''){
            $connar['where'].=$this->sqlLikeEscape(' and name like "%??%"',array($_POST['class_name']));
        }
        if(isset($_SESSION[SESS_ASSESS]['teacher_code'])&&$_SESSION[SESS_ASSESS]['teacher_code'] != ''){
            $connar['where'].=$this->sqlLikeEscape(' and teacher_id=??',array($_SESSION[SESS_ASSESS]['teacher_code']));
         }
        if(isset($_POST['teacher_code'])&&$_POST['teacher_code'] != ''){
            $connar['where'].=$this->sqlLikeEscape(' and teacher_id=??',array($_POST['teacher_code']));
        }
        if(isset($_POST['course_units'])&&$_POST['course_units'] != ''){
            $connar['where'].=$this->sqlLikeEscape(' and course_units="??"',array($_POST['course_units']));
        }
        
        if(isset($_POST['listen_time'])&&$_POST['listen_time'] != ''){
            $weekday=$weekarray[date("w",strtotime($_POST['listen_time']))];
            $quantumsw=date('H',strtotime($_POST['listen_time']));
            $quantumsw=$quantumsw<=12 ? '上午' :'下午';
            $connar['where'].=$this->sqlLikeEscape(' and sksj1 like "%??%"',array($weekday.$quantumsw));
        }
        $connar['join']=array('db_admin','db_admin.usercode=db_class.teacher_id');
        $connar['items']="name,db_class.id,username,db_admin.dept_name,sksj";
        $connar['groupby']="name";
        $data=$this->Class_model->get_list_full($connar);

        foreach ($data as $k => $v) {
            if(!$v['username']){
                $data[$k]['username']='';
            }
            if(!$v['dept_name']){
                $data[$k]['dept_name']='';
            }else{
                $data[$k]['dept_name']='--'.$v['dept_name'];
            }
        }
        exit(retJson('返回搜索结果',true,$data)) ; 
    }

    

	function get_string_between($string, $start, $end){
		$string = ' ' . $string;
		$ini = strpos($string, $start);
		if ($ini == 0) return '';
		$ini += strlen($start);
		$len = strpos($string, $end, $ini) - $ini;
		return substr($string, $ini, $len);
	}
    // 0507 课程信息session保存
    function setClass(){
         $this->load->model("Class_model");
        if(isset($_POST['class_name'])){
            $_SESSION[SESS_ASSESS]['class_name']=$_POST['class_name'];
            $month=date('m',time());
            if($month==9 || $month==10 || $month==11 || $month==12 || $month==1){
                $year=date('Y',time());
                $year_last=date('Y',time())+1;
                $default_academic_year=strval($year.'-'.$year_last);            
            }else if($month==2 || $month==3 || $month==4 || $month==5 || $month==6 || $month==7 || $month==8){
                $year=date('Y',time())-1;
                $year_last=date('Y',time());
                $default_academic_year=strval($year.'-'.$year_last);    
            }
            $connar['where']=$this->sqlLikeEscape('name like "%??%" and elective_lesson_no like "%??%"',array($_POST['class_name'],$default_academic_year));
            if(isset($_SESSION[SESS_ASSESS]['teacher_code'])){
                $connar['where'].=$this->sqlLikeEscape(' and teacher_id="??"',array($_SESSION[SESS_ASSESS]['teacher_code']));
            }
            $connar['join']=array('db_admin a','a.usercode=db_class.teacher_id');
            $connar['items']="db_class.id,db_class.classname,db_class.course_units,db_class.type,db_class.classroom,db_class.teacher_id,a.username,a.dept_name,a.title,a.age_groups,a.teachers_attribute";
            $data=$this->Class_model->get_single_full($connar);
            if($data){
                $_SESSION[SESS_ASSESS]['class_id']=$data['id'];
                $_SESSION[SESS_ASSESS]['classtype']=$data['type'];
                $_SESSION[SESS_ASSESS]['classname']=$data['classname'];
                /* $_SESSION[SESS_ASSESS]['course_units']=$data['course_units'];
                $_SESSION[SESS_ASSESS]['teacher_id']=$data['teacher_id'];
                $_SESSION[SESS_ASSESS]['teacher_name']=$data['username'];
                $_SESSION[SESS_ASSESS]['dept_name']=$data['dept_name'];
                $_SESSION[SESS_ASSESS]['title']=$data['title'];
                $_SESSION[SESS_ASSESS]['age_groups']=$data['age_groups'];
                $_SESSION[SESS_ASSESS]['teachers_attribute']=$data['teachers_attribute']; */
                if($data['classroom']){
                    $_SESSION[SESS_ASSESS]['classroom']=explode(';',$data['classroom'])[0];
                }
                
            }
            exit(retJson('保存成功',true)); 
        }
        exit(retJson('系统错误',false)); 
    }

    function changeSksj(){
        $this->load->model("Class_model");
        $month=date('m',time());
        if($month==9 || $month==10 || $month==11 || $month==12 || $month==1){
            $year=date('Y',time());
            $year_last=date('Y',time())+1;
            $default_academic_year=strval($year.'-'.$year_last);            
        }else if($month==2 || $month==3 || $month==4 || $month==5 || $month==6 || $month==7 || $month==8){
            $year=date('Y',time())-1;
            $year_last=date('Y',time());
            $default_academic_year=strval($year.'-'.$year_last);    
        }
        $connar['where']=$this->sqlLikeEscape('elective_lesson_no like "%??%"',array($default_academic_year));
        $connar['items']="name,db_class.id,sksj";
        $batch_sql='';
        $data=$this->Class_model->get_list_full($connar);
        foreach ($data as $k => $v) {
            $sksj=explode(';',$v['sksj']);
            $sj='';
            
            foreach ($sksj as $k1 => $v1) {
                $quantumsw= substr($v1,9,1)<=4 ? '上午' :'下午';
                $weekday=substr($v1,0,6);
                $sj.=$weekday.$quantumsw.";";
            }
            $batch_sql.= "update db_class set sksj1='".$sj."' where id=".$v['id'].";";
        }
        echo $batch_sql;
    }

}